package com.samoilovich.belarustoday.afisha.films;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.basefragments.BaseFragment;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.FilmModel;
import com.samoilovich.belarustoday.tools.Utility;

public class FilmDescriptionFragment extends BaseFragment implements ImageLoadingListener {
    private static FilmModel mCurrentFilm;
    private ProgressBar mProgressBarPoster;
    private ImageView mImageViewPoster;

    public FilmDescriptionFragment() {

    }

    public FilmDescriptionFragment(FilmModel film) {
        mCurrentFilm = film;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.film_description, null);
        prepareActionBarViews(R.string.empty_str, R.string.films, true);
        prepareViews();

        return mRootView;
    }

    @Override
    protected void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        updatePoster();
    }

    @Override
    public void updateAllData(Context context) {
        updatePoster();
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void prepareViews() {
        if (mCurrentFilm != null) {
            ((TextView) mRootView.findViewById(R.id.textView_name)).setText(mCurrentFilm.getName());
            ((TextView) mRootView.findViewById(R.id.textView_year)).setText(mCurrentFilm.getYear());
            ((TextView) mRootView.findViewById(R.id.textView_genre)).setText(mCurrentFilm.getGenre());
            ((TextView) mRootView.findViewById(R.id.textView_director)).setText(mCurrentFilm.getDirector());
            ((TextView) mRootView.findViewById(R.id.textView_age_limit)).setText(mCurrentFilm.getAgeLimit());
            ((TextView) mRootView.findViewById(R.id.textView_duration)).setText(mCurrentFilm.getTime());
            ((TextView) mRootView.findViewById(R.id.textView_description)).setText(mCurrentFilm.getDescription());
            updatePoster();
        }
    }

    @Override
    public void onLoadingStarted(String imageUri, View view) {

    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        mImageViewPoster.setVisibility(View.VISIBLE);
        mProgressBarPoster.setVisibility(View.GONE);
        mImageViewPoster.setImageResource(R.drawable.no_image);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        view.setVisibility(View.VISIBLE);
        mProgressBarPoster.setVisibility(View.GONE);
        mCurrentFilm.setPosterDrawable(((ImageView) view).getDrawable());
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {

    }

    // ---------------------------------------------------------------------------------------------
    private void updatePoster() {
        boolean pictureLoadingState = Utility.getPictureLoadingState(getActivity());

        if (mImageViewPoster == null || mProgressBarPoster == null) {
            mImageViewPoster = (ImageView) mRootView.findViewById(R.id.imageView_poster);
            mProgressBarPoster = (ProgressBar) mRootView.findViewById(R.id.progressBar_poster);
        }

        if (mCurrentFilm.getPosterDrawable() != null && mCurrentFilm.getPosterDrawable().get() != null) {
            mImageViewPoster.setVisibility(View.VISIBLE);
            mProgressBarPoster.setVisibility(View.GONE);
            mImageViewPoster.setImageDrawable(mCurrentFilm.getPosterDrawable().get());
        } else {
            if (pictureLoadingState) {
                if (mCurrentFilm.getPosterDrawable() == null) {
                    if (Utility.hasInternetConnection(getActivity())) {
                        mImageViewPoster.setVisibility(View.GONE);
                        mProgressBarPoster.setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().denyNetworkDownloads(false);
                        ImageLoader.getInstance().displayImage(mCurrentFilm.getPoster(), mImageViewPoster, this);
                    } else {
                        mImageViewPoster.setVisibility(View.GONE);
                        mProgressBarPoster.setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().denyNetworkDownloads(true);
                        ImageLoader.getInstance().displayImage(mCurrentFilm.getPoster(), mImageViewPoster, this);
                    }
                } else {
                    mImageViewPoster.setVisibility(View.VISIBLE);
                    mProgressBarPoster.setVisibility(View.GONE);
                    mImageViewPoster.setImageDrawable(mCurrentFilm.getPosterDrawable().get());
                }
            } else {
                mImageViewPoster.setVisibility(View.GONE);
                mProgressBarPoster.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().denyNetworkDownloads(true);
                ImageLoader.getInstance().displayImage(mCurrentFilm.getPoster(), mImageViewPoster, this);
            }
        }
    }
}